package com.tarena.lbs.message.web.controller;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.common.passport.principle.UserPrinciple;
import com.tarena.lbs.common.security.utils.LbsSecurityContenxt;
import com.tarena.lbs.message.web.service.MsgService;
import com.tarena.lbs.pojo.message.dos.Msg;
import com.tarena.lbs.pojo.message.dos.TriggerMsgActionTemplate;
import com.tarena.lbs.pojo.message.param.TriggerMessageQueryParam;
import com.tarena.lbs.pojo.message.query.MessageQuery;
import com.tarena.lbs.pojo.message.vo.MessageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 消息管理控制器
 */
@RestController
@RequestMapping("/message")
@Api(tags = "消息管理接口")
@CrossOrigin
public class MessageController {

    @Autowired
    MsgService msgService;

    /**
     * 查询某个用户最新消息
     *
     * @param messageQuery 消息查询对象
     * @return Result<List<Msg>>对象
     */
    @ApiOperation("查询某个用户最新消息")
    @PostMapping("/selectUserNewMessage")
    public Result<List<MessageVo>> selectUserNewMessage(@RequestBody MessageQuery messageQuery) {
        List<MessageVo> msgList = msgService.selectUserNewMessage(messageQuery);
        return new Result<>(msgList);
    }

    /**
     * 更新消息为已读状态
     *
     * @param messageId 消息编号
     * @return Result<Boolean>对象
     */
    @ApiOperation("更新消息为已读状态")
    @PostMapping("/updateMsgStatus")
    @ApiImplicitParam(name = "messageId", value = "消息编号")
    public Result<Boolean> updateMsgStatus(Integer messageId) {
        Msg msg = new Msg();
        msg.setId(messageId);
        msg.setStatus(2);
        UserPrinciple loginUser = LbsSecurityContenxt.getLoginToken();
        msg.setUserId(loginUser.getId());
        boolean result = msgService.updateMsgStatus(msg);
        return new Result<>(result);
    }

    /**
     * 删除消息
     *
     * @param messageId 消息编号
     * @return Result<Boolean>对象
     */
    @ApiOperation("删除消息")
    @PostMapping("/deleteById")
    @ApiImplicitParam(name = "messageId", value = "消息编号")
    public Result<Boolean> deleteById(Long messageId) {
        boolean result = msgService.deleteById(messageId);
        return new Result<>(result);
    }

    /**
     * 查询所有消息
     *
     * @return Result<List<Msg>>对象
     */
    @ApiOperation("查询所有消息")
    @PostMapping("/selectAll")
    public Result<List<Msg>> selectAll() {
        List<Msg> msgList = msgService.selectAll();
        return new Result<>(msgList);
    }

    @ApiOperation("查询行为触发")
    @PostMapping("/getActionAll")
    public Result<BasePageList<TriggerMsgActionTemplate>> getActionAll(@RequestBody TriggerMessageQueryParam param) {
        return new Result<>(msgService.getActionAll(param));
    }
}
